package Math;

import java.math.BigDecimal;

public class _172_FactorialTrailingZeroes {
    //first solution:time limit exceeded
    public int trailingZeroes_0(int n) {
        int count = 0;
        BigDecimal number =  new BigDecimal(1);
        boolean trail = true;
        while (n > 0) {
            number = number.multiply(new BigDecimal(n));
            n = n-1;
        }
        String nString = number + "";
        System.out.println(nString);
        for(int i=nString.length()-1;i>=0;i--) {
            if (trail) {
                if (nString.charAt(i) == '0') {
                    count++;
                } else {
                    trail = false;
                }
            } else {
                break;
            }
        }
        return count;
    }

    //solution 2:using directly calcilate,over time limited
    public int trailingZeroes_1(int n) {
        int count = 0;
        int num = 1;
        BigDecimal total = new BigDecimal(1);
        while (num <= n) {
            if (num % 10 == 0) {
                count += Math.log10(num);
            } else if (num % 5 == 0) {
                count += 1;
            }
            num += 1;
            total = total.multiply(new BigDecimal(num));
        }
        return count;
    }

    //reference solution:get 5 nad 5's multiply's count
    public int trailingZeroes(int n) {
        return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
    }
}
